package com.power.sifa.net.controller;

import com.plf.common.domain.ResponseDTO;
import com.plf.common.exception.BusinessException;
import com.plf.common.utils.SpringHolder;
import com.plf.core.common.file.FileController;
import com.power.sifa.net.api.IApl_formFeign;
import com.power.sifa.net.model.EqproFormTreeNode;
import com.power.sifa.net.model.GeneralBuild;
import com.power.sifa.net.model.SiteTreeBuild;
import com.power.sifa.net.service.Apl_formService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@RestController
@Api(value = "Apl_storageController", tags = {"表单数据接口"})
public class Apl_formController implements IApl_formFeign {

    @Resource
    private Apl_formService apl_formService;

    @Override
    public ResponseDTO saveFormTreeData(EqproFormTreeNode treeNode) throws Exception {
        Assert.notNull(treeNode.getSign(), "未传入节点标识");
        Assert.notNull(treeNode.getBody(), "未传入保存对象");

        apl_formService.saveFormTreeData(treeNode);
        return ResponseDTO.succ();
    }

    @Override
    public ResponseDTO delFormTreeData(Integer sign, String table, String id) {
        Assert.notNull(table, "未传入节点表名");
        Assert.notNull(table, "未传入节点id");
        this.apl_formService.delFormTreeData(sign, table, id);
        return ResponseDTO.succ();
    }

    @Override
    public ResponseDTO getSelectTree(String clsname, Map<String, Object> filter) throws Exception {
        Assert.notNull(clsname, "未传入类名");
        return ResponseDTO.succData(apl_formService.getSelectTree(clsname, filter));
    }

    @Override
    public ResponseDTO buildSiteTree(String applyid, SiteTreeBuild terrBuild) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Assert.notNull(applyid, "未传入申请表id");
        Assert.notNull(terrBuild, "未传入树构造配置");
        Assert.notNull(terrBuild.getSitetable(), "未传入场地表");

        return ResponseDTO.succData(this.apl_formService.buildSiteTree(applyid, terrBuild));
    }

    @Override
    public ResponseDTO getGeneralTree(String applyid, GeneralBuild generalBuild) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {

        Assert.notNull(generalBuild.getApltabl(), "未传入通用构造apltalbl");
        Assert.notNull(generalBuild.getCornumber(), "未传入通用构造cornumber");
        Assert.notNull(generalBuild.getIsAdd(), "未传入通用构造isadd");
        Assert.notNull(generalBuild.getMaprelation(), "未传入通用构造maprelation");
        Assert.notNull(generalBuild.getEnttabl(), "未传入通用构造enttabl");


        return ResponseDTO.succData(this.apl_formService.getGeneralTree(applyid, generalBuild));
    }

    @Override
    public ResponseDTO delGeneralNode(String applyid, String id, String apltable, String enttable) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, BusinessException {
        return ResponseDTO.succData(this.apl_formService.delGeneralNode(applyid, id, apltable, enttable));
    }

    @Override
    public ResponseDTO saveGeneralNode(String apltable, String enttable, Map body) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        return ResponseDTO.succData(this.apl_formService.saveGeneralNode(apltable, enttable, body));
    }

    @Override
    public ResponseDTO getEqTree(String cornumber, SiteTreeBuild terrBuild) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Assert.notNull(cornumber, "未传入社会统一信用代码");
        Assert.notNull(terrBuild, "未传入树构造配置");
        Assert.notNull(terrBuild.getSitetable(), "未传入场地表");
        return ResponseDTO.succData(this.apl_formService.getEqTree(cornumber, terrBuild));
    }

    @Override
    public ResponseDTO queryStdByIds(String table, String ids) {
        return ResponseDTO.succData(this.apl_formService.queryStdByIds(table, ids));
    }

    @Override
    public ResponseDTO getFile(String id) {
        FileController fileController = SpringHolder.getBean(FileController.class);
        ResponseDTO result = fileController.get(id);
        return result;
    }
}
